<template>
  <div class="tabs">
    <el-tag v-for="(tag, index) in tags" :key="tag.name" :closable="tag.name !== 'home'"
            :disable-transitions="false" :effect="$route.name === tag.name ? 'dark' : 'plain'" @click="changeMenu(tag)"
            @close="handleClose(tag, index)" size="small">
      {{ tag.label }}
    </el-tag>
  </div>
</template>

<script setup>
import {useStore} from 'vuex';
import {useRouter, useRoute} from 'vue-router';
import {onUpdated, onMounted, computed, reactive} from "vue"

const store = useStore();
const router = useRouter();
const route = useRoute();
const tags = store.state.tabsList
// onMounted(() => {
//   console.log( tags)
// })
const changeMenu = (item) => {
  console.log("tags", item)
  router.push({
    name: item.name
  })
};
const handleClose = (tag, index) => {
  let length = tags.length - 1;
  // 处理vuex 中的tabsList
  store.commit("closeTab", tag);
  // 做第一个判断
  // 如果是第一个就不做处理
  if (tag.name !== route.name) {
    return;
  }

  if (index === length) {
    router.push({
      name: tags[index - 1].name,
    });
  } else {
    router.push({
      name: tags[index].name,
    });
  }
};
</script>
<style lang="less" scoped>
.tabs {
  padding: 10px;

  .el-tag {
    margin-left: 15px;
    cursor: pointer;
  }
}
</style>